import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';

const baseUrl = '/parts/api/v1';

const initDesc = formatMessage({
    id: 'api.message.initDesc',
    defaultMessage: '初始化界面'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询备件索赔报告'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询备件索赔报告详情'
});
const confirmtDesc = formatMessage({
    id: 'api.message.exportDesc',
    defaultMessage: '确认备件索赔报告'
});
const newOrderDesc = formatMessage({
    id: 'api.message.newOrderDesc',
    defaultMessage: '新增备件索赔报告'
});
const modifyDesc = formatMessage({
    id: 'api.message.modifyDesc',
    defaultMessage: '修改备件索赔报告'
});
const submitDataDesc = formatMessage({
    id: 'api.message.submitDataDesc',
    defaultMessage: '提交备件索赔报告'
});
const abandonDataDesc = formatMessage({
    id: 'api.message.abandonDataDesc',
    defaultMessage: '作废备件索赔报告'
});
const getPartInvoiceDesc = formatMessage({
    id: 'api.message.getPartInvoiceDesc',
    defaultMessage: '查询备件箱单发票'
});
const printDataDesc = formatMessage({
    id: 'api.message.printDataDesc',
    defaultMessage: '打印备件箱单发票'
});

//初始化
export const getInitData = () => request(`${baseUrl}/ui/partClaimReport/init`, null, dto.InitDataStruct)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, initDesc));
// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partClaimReports/forCurrentUser?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/partClaimReports/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
//新增
export const newOrder = data => request(`${baseUrl}/partClaimReports`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newOrderDesc));
//修改
export const modifyOrder = (id, data) => request(`${baseUrl}/partClaimReports/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyDesc));
//提交
export const submitData = (id, data) => request(`${baseUrl}/partClaims/${id}/submit`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitDataDesc));
//作废
export const abandonData = (id, data) => request(`${baseUrl}/partClaimReports/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDataDesc));
//确认
export const confirmData = (id, data) => request(`${baseUrl}/partClaimReports/${id}/confirm`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, confirmtDesc));
// 查询备件索赔单
export const partInvoicesQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/partClaims/forPartClaimReport?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPartInvoiceDesc));
};
//打印
export const printData = (id, data) => request(`${baseUrl}/partClaimReports/${id}/print`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, printDataDesc));
